package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.constant.MessageConstant;
import com.atguigu.common.entity.PageResult;
import com.atguigu.common.entity.QueryPageBean;
import com.atguigu.common.entity.Result;
import com.atguigu.pojo.Address;
import com.atguigu.service.AddressService;
import com.atguigu.vo.AddressAddVo;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    AddressService addressService;


    @GetMapping("/findAllMaps")
    public Map findAllMaps(){
        Map map=new HashMap();
        List<Address> addressList = addressService.findALLMaps();

        //1、定义分店坐标集合
        List<Map> gridnMaps=new ArrayList<>();
        //2、定义分店名称集合
        List<Map> nameMaps=new ArrayList();
        for (Address address : addressList) {
            Map gridnMap=new HashMap();
            // 获取经度
            gridnMap.put("lng",address.getLng());
            // 获取纬度
            gridnMap.put("lat",address.getLat());
            gridnMaps.add(gridnMap);

            Map nameMap=new HashMap();
            // 获取地址的名字
            nameMap.put("addressName",address.getAddressName());
            nameMaps.add(nameMap);
        }
        // 存放经纬度
        map.put("gridnMaps",gridnMaps);
        // 存放名字
        map.put("nameMaps",nameMaps);
        return map;
    }

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult addresses =addressService.findPage(queryPageBean);
        return new Result(true, "成功",addresses);
    }

    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody AddressAddVo addressAddVo){
        addressService.addAddress(addressAddVo.getAddressName(),addressAddVo.getLng(),addressAddVo.getLng());
        System.out.println("11111111");
        System.out.println("22222");
        System.out.println("-------------");
        return new Result(true,"添加成功");
    }
}
